package org.lzuploader.gui.listeners.options;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Shell;
import org.lzuploader.gui.WMain;
import org.lzuploader.gui.WOptions;
import org.lzuploader.gui.helpers.EasyMessageBox;
import org.lzuploader.model.Config;
import org.lzuploader.model.Localization;

/** This class is used when the user validates his modifications on options page.
 * 
 * @author Emmanuel Cron
 *
 */
public class OkSelectionListener implements SelectionListener
{
    /** The logger of this class. */
    private Log log = LogFactory.getLog(OkSelectionListener.class);
    
    /** Localization of this class. */
    private Localization localization = Localization.getInstance();
    
    /** Used when widget is selected by default.
     * @param e Associated event
     */
    @Override
    public void widgetDefaultSelected(final SelectionEvent e)
    {
        widgetSelected(e);
    }

    /** Used when widget is manually selected.
     * @param e Associated event
     */
    @Override
    public void widgetSelected(final SelectionEvent e)
    {
        log.debug("Validating options.");
        
        log.trace("Fetching data.");
        
        Shell wOptionsShell = null;
        for (int i = 0 ; i < e.display.getShells().length ; i++)
        {
            if (e.display.getShells()[i].getData() instanceof WOptions)
            {
                wOptionsShell = e.display.getShells()[i];
                break;
            }
        }
        
        final WOptions options = (WOptions) wOptionsShell.getData();
        
        String localeId = null;
        
        for (int i = 0 ; i < Localization.LOCALES.length ; i++)
        {
            if (Localization.LOCALES[i].getLiteral().equals(options.getLanguageWidget().getText()))
            {
                localeId = Localization.LOCALES[i].getId();
                break;
            }
        }
        
        log.trace("Getting config.");
        
        final Config config = Config.getInstance();
        
        log.trace("Checking if changes have been made.");
        
        if ( (options.getWowPathWidget().getText().length() > 0 && !options.getWowPathWidget().getText().toString().equals(config.getWowPath())) || !localeId.equals(config.getLocalization()) )
        {
            log.trace("Saving some changes.");
            
            config.setWowPath(options.getWowPathWidget().getText().toString());
            
            if (!localeId.equals(config.getLocalization()))
            {
                config.setLocalization(localeId);
                
                EasyMessageBox.info(
                    localization.getString("DIALOG_TITLE_INFO"),
                    localization.getString("DIALOG_LOCALE_CHANGED")
                );
            }
            
            log.trace("Saving to file.");
            
            if (!config.save())
            {
                EasyMessageBox.error(
                    localization.getString("DIALOG_TITLE_ERROR"),
                    localization.getString("DIALOG_SAVE_FAILED")
                );
            }
            
            // Refreshing components of main window
            final WMain main = (WMain) wOptionsShell.getParent().getShell().getData();
            main.refreshComponents();
        }
        else
        {
            log.trace("Nothing has changed.");
        }
        
        log.trace("Closing window.");
        
        wOptionsShell.dispose();
    }

}
